/**
 *  Copyright 2002 Quaseem
 *  Copyright 2004 Migisan
 *  Copyright 2008 Oest
 *  
 *
 *  This file is part of the aochat package.
 *
 *  The aochat package is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The aochat package is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with the aochat package; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package sk.sigp.aobot.client.core.message;

import sk.sigp.aobot.client.core.User;
import sk.sigp.aobot.client.core.message.general.Message;
import sk.sigp.aobot.client.types.ChatGroupId;
import sk.sigp.aobot.client.types.GroupId;
import sk.sigp.aobot.client.types.Item;
import sk.sigp.aobot.client.types.Raw;
import sk.sigp.aobot.client.types.Text;

public class ChatMessage {
	protected int messageType;
	public User usr;
	public GroupId gid;
	public ChatGroupId cgid;
	public String text;
	public String potentialNick;
	protected String textblob = null;
	protected Item item = null;

	public int type;
	protected long producedTime;

	public static final int MESSAGE_TYPE_PRIVATE = 0;
	public static final int MESSAGE_TYPE_GROUP = 1;
	public static final int MESSAGE_TYPE_PRIVATEGROUP = 2;

	public ChatMessage(User usr, GroupId gid, ChatGroupId cgid, String text,
			Raw raw, String blob, Item item) {
		this.usr = usr;
		this.gid = gid;
		this.cgid = cgid;

		type = MESSAGE_TYPE_PRIVATE;
		if (gid != null)
			type = MESSAGE_TYPE_GROUP;
		if (cgid != null)
			type = MESSAGE_TYPE_PRIVATEGROUP;

		this.text = "" + text;
		if (raw != null) {
			if (raw.size() > 20) {
				if (raw.bytesToInt(2) == 0x0000C350)
					this.textblob = raw.bytesToString(16);
				else if (raw.bytesToInt(1) == 0x0000C350)
					this.textblob = raw.bytesToString(15);
			} else if (raw.size() == 20)
				this.item = new Item(raw);
		} else {
			this.textblob = blob;
			this.item = item;
		}
		this.producedTime = System.currentTimeMillis();
	}

	public ChatMessage(User usr, String text, Raw raw) {
		this(usr, null, null, text, raw, null, null);
	}

	public ChatMessage(User usr, String text, String blob) {
		this(usr, null, null, text, null, blob, null);
	}

	public ChatMessage(User usr, GroupId gid, String text, Raw raw) {
		this(usr, gid, null, text, raw, null, null);
	}

	public ChatMessage(User usr, ChatGroupId cgid, String text, Raw raw) {
		this(usr, null, cgid, text, raw, null, null);
	}

	public GroupId getGroupID() {
		return gid;
	}

	public void setGroupID(GroupId g) {
		gid = g;
	}

	public ChatGroupId getChatgroupID() {
		return cgid;
	}

	public void setChatgroupID(ChatGroupId cg) {
		cgid = cg;
	}

	public int getType() {
		return type;
	}

	public Message[] toMessages() {
		Message msg = null;
		switch (type) {
		case MESSAGE_TYPE_PRIVATE:
			msg = new Message(Message.M_C_MESSAGE_PRIVATE);
			msg.setElementAt(0, usr.user_id);
			break;
		case MESSAGE_TYPE_GROUP:
			msg = new Message(Message.M_S_GROUP_MESSAGE);
			msg.setElementAt(0, gid);
			break;
		case MESSAGE_TYPE_PRIVATEGROUP:
			msg = new Message(Message.M_C_PRIVATEGROUP_MESSAGE);
			msg.setElementAt(0, cgid);
			break;
		}
		msg.setElementAt(1, new Text(text));
		if (item != null)
			msg.setElementAt(2, new Raw(item));
		else if (textblob != null)
			msg.setElementAt(2, new Raw(textblob));
		else
			msg.setElementAt(2, new Raw());
		Message[] ret = new Message[1];
		ret[0] = msg;
		return ret;
	}
}
